//
//  ViewController.m
//  InfiniteLoop
//
//  Created by 徐冉 on 2018/12/13.
//  Copyright © 2018年 LG. All rights reserved.
//

#import "ViewController.h"
#import "LGCycleScrollView.h"
#import "LgImageColorModel.h"

@interface ViewController ()<SDCycleScrollViewDelegate>

//背景颜色视图
@property (strong, nonatomic) UIView *backgroundView;
//遮罩层  用来实现颜色渐变 不突兀
@property (strong, nonatomic) UIView *maskView;
@property (strong, nonatomic) NSMutableArray *imgModelArray;

@end

@implementation ViewController

- (NSMutableArray *)imgModelArray {
    if (_imgModelArray == nil) {
        _imgModelArray = [[NSMutableArray alloc] init];
    }
    return _imgModelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // 颜色视图
    _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 400)];
    _backgroundView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_backgroundView];
    
    // 遮罩层
    _maskView = [[UIView alloc] initWithFrame:_backgroundView.bounds];
    _maskView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_maskView];

    NSArray *imgArray = @[
                          @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544788655016&di=aa45f6891f19da33a67d957ce780aec9&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fwallpaper%2F1212%2F24%2Fc0%2F16842812_1356321417617.jpg",
                          @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544788655013&di=1a428f50ea3410653bea796a7bd03b1e&imgtype=0&src=http%3A%2F%2Fs9.knowsky.com%2Fbizhi%2Fl%2F35001-45000%2F200952941737825036882.jpg",
                          @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544788655013&di=a06fd5cf1aeaf50500743f428c35daa8&imgtype=0&src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fwallpaper%2F1210%2F08%2Fc3%2F14310247_1349685003502.jpg",
                          @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544788655119&di=7649b575fdddd845b3ecb56b40b52b62&imgtype=0&src=http%3A%2F%2Fimg.article.pchome.net%2F00%2F29%2F46%2F52%2Fpic_lib%2Fwm%2Fweimei_25.jpg",
                          @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544788655116&di=05e8a88ad6269e82182deebd76e16ffa&imgtype=0&src=http%3A%2F%2Fimg17.3lian.com%2Fd%2Ffile%2F201702%2F27%2F51b039cb30635e81b3cbce7ccd019f8c.jpg",
                          @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1544788836501&di=8dd1d3d6ab9b576c7766d24d45d66638&imgtype=0&src=http%3A%2F%2Fpic28.photophoto.cn%2F20130720%2F0033034027352939_b.jpg"];

    CGFloat scrollViewWidth = self.view.frame.size.width;

    LGCycleScrollView *scrollView = [LGCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 100, scrollViewWidth, 300) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    scrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    scrollView.autoScrollTimeInterval = 5.0;
    scrollView.imageURLStringsGroup = imgArray;
    [self.view addSubview:scrollView];

    for (NSString *name in imgArray) {
        LgImageColorModel *model = [[LgImageColorModel alloc] init];
        model.imageURLString = name;
        [self.imgModelArray addObject:model];
    }
    
    // 初始为第一个颜色值
    if (self.imgModelArray.count > 0) {
        self.maskView.backgroundColor = [self getImageColorWithIndex:0];
        self.backgroundView.backgroundColor = [self getImageColorWithIndex:0];
    }
    
    
    __weak LGCycleScrollView *scroll = scrollView;
    __weak typeof(self) weakSelf = self;
    
#pragma mark - 滚动视图  滚动代理 滚动停止时回调
    scrollView.scrollViewDidEndScrollBlock = ^(UIScrollView * scrollV) {

        NSInteger currentIndex = scroll.curIndex;
        weakSelf.maskView.backgroundColor = [self getImageColorWithIndex:currentIndex];
        weakSelf.maskView.alpha = 1.0;
//        NSLog(@"----->currentIndex:%ld",currentIndex);
    };

#pragma mark - 滚动视图  滚动代理 多次回调
    scrollView.scrollViewDidScrollBlock = ^(UIScrollView * scrollV) {

        CGFloat currentIndex = scroll.curIndex;
        //计算偏移量差值
        double offset = fabs((double)scrollV.contentOffset.x - scroll.lastOffset.x);
        //计算颜色渐变系数
        double alpha = offset / 100 * 0.2667;
//        NSLog(@"offset:%.f",offset);
//        NSLog(@"currentIndex:%.f",currentIndex);

        
        if (scroll.direction == LGCycleScrollViewDirectionLeft) {//向左
//            NSLog(@"letft--->alpha:%.2f", 1 - alpha);
            weakSelf.maskView.alpha = 1 - alpha;
            if (offset <= scrollViewWidth / 2) {
                if ((currentIndex + 1) >= weakSelf.imgModelArray.count) {//当前最后一个颜色 取第一个颜色值
                    weakSelf.backgroundView.backgroundColor = [self getImageColorWithIndex: 0];
                }else {
                    
                    weakSelf.backgroundView.backgroundColor = [self getImageColorWithIndex:currentIndex + 1];
                }
            }
        }else {//向右
//            NSLog(@"right--->alpha:%.2f", 1 - alpha);
            weakSelf.maskView.alpha = 1 - alpha;
            if (offset <= scrollViewWidth / 2) {
                if ((currentIndex - 1) < 0 ) {//当前第一个颜色  取最后一个颜色值
                    weakSelf.backgroundView.backgroundColor = [self getImageColorWithIndex: weakSelf.imgModelArray.count - 1];
                }else {
                    
                    weakSelf.backgroundView.backgroundColor = [self getImageColorWithIndex:currentIndex - 1];
                }
            }
        }

    };

}

#pragma mark - 根据索引获取图片颜色
-(UIColor *)getImageColorWithIndex:(NSInteger)index {
    
    if (index >= self.imgModelArray.count) {//防止越界崩溃
        return [UIColor clearColor];
    }
    LgImageColorModel *model = [self.imgModelArray objectAtIndex:index];
    if (model.downloadImage != nil) {
        if (model.imageColor != nil) {
//            NSLog(@"直接获取颜色");
            return model.imageColor;
        }else {
//            NSLog(@"二次获取颜色");
            return [model getImageColor];
        }
    }else {
//        NSLog(@"下载图片获取颜色");
        return [model downloadNetworkImage];
        
    }
    
}

@end
